package view;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import model.dateModel;

/**
 * The Class ViewMonthlyGUI that extends AbstractGUI.
 * @author yudku
 */
public class ViewMonthlyGUI extends AbstractGUI
{
	
	/** The report combo. */
	private JComboBox reportCombo =null;
	
	/** The date combo. */
	private JComboBox dateCombo =null;
	
	/** The date combo2. */
	private JComboBox dateCombo2 =null;
	
	/** The rdbtn compare. */
	private JCheckBox rdbtnCompare = null;
	
	/** The content pane. */
	private JPanel contentPane=null;
	
	/** The wrn message. */
	private JLabel wrnMessage=null;
	
	/** The compare button. */
	private JButton compareButton = null;
	
	/** The comp date array. */
	private ArrayList<dateModel> compDateArray=null;
	
	/** The arch date array. */
	private ArrayList<dateModel> archDateArray=null;
	
	/** The pack date array. */
	private ArrayList<dateModel> packDateArray=null;
	
	/** The ind. */
	private int ind = 0;
	
	/** The date. */
	private ArrayList <String> DATE = new ArrayList <String> ();
	
	/** The comp. */
	private boolean comp = false;
	
	/**
	 * Instantiates a new view monthly gui.
	 */
	public ViewMonthlyGUI() {
		// Set JFrame's layout
		contentPane=new JPanel();	
		contentPane.setBorder(new TitledBorder(null, "Monthly Report", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.setLayout(null);
		contentPane.setOpaque(true);
		contentPane.add(getWrnMessage());
		contentPane.add(getBackButton());
		contentPane.add(getCompareButton());    		
		
		// Set a report type label
		JLabel lblReportType = new JLabel("Report type:");
		lblReportType.setBounds(50, 25, 95, 14);
		contentPane.add(lblReportType);
		
		// Set Choose date label
		JLabel lblChooseDate = new JLabel("Choose date:");
		lblChooseDate.setBounds(50, 105, 90, 14);
		contentPane.add(lblChooseDate);
	
		// Set a compare to label
		JLabel lblCompareTo = new JLabel("Compare to:");
		lblCompareTo.setBounds(250, 105, 90, 14);
		contentPane.add(lblCompareTo);
		
		// Set Report type combo box 
		reportCombo = new JComboBox();
		reportCombo.setBounds(50, 50, 175, 20);
		reportCombo.setEnabled(true);
		contentPane.add(reportCombo);		
		
		// Set date combo box
		dateCombo = new JComboBox();
		dateCombo.setBounds(50, 120, 175, 20);
		dateCombo.setEnabled(false);
		contentPane.add(dateCombo);
		
		dateCombo2 = new JComboBox();
		dateCombo2.setBounds(250, 120, 175, 20);
		dateCombo2.setEnabled(false);
		contentPane.add(dateCombo2);
		
		// Set a compare label
		rdbtnCompare = new JCheckBox("Compare");
		rdbtnCompare.setBounds(370, 95, 90, 14);
		rdbtnCompare.setEnabled(true);
		rdbtnCompare.setVisible(true);
		rdbtnCompare.addActionListener(new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(rdbtnCompare.isSelected())
					dateCombo2.setEnabled(true);
				else
					dateCombo2.setEnabled(false);
				
			}
			
		});
		contentPane.add(rdbtnCompare);
		
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setTitle("Monthly Report Window");
        this.setContentPane(contentPane);      		           
	    this.setLocationRelativeTo(null); 
        this.setLocationByPlatform(true);
        this.setSize(500, 299); 	
	}
	
	/**
	 * Gets the compare button.
	 *
	 * @return the compare button
	 */
	private JButton getCompareButton() {
		if (compareButton == null) {
			compareButton = new JButton();
			compareButton.setBounds(new Rectangle(72, 174, 100, 35));
			compareButton.setText("View ");
			
		}
		return compareButton;
	}
	
	/**
	 * Gets the wrn message.
	 *
	 * @return the wrn message
	 */
	private JLabel getWrnMessage() {
		if (wrnMessage == null) {
			wrnMessage = new JLabel();
			wrnMessage.setForeground(Color.RED);
			wrnMessage.setBounds(70, 16, 320, 23);
			wrnMessage.setVisible(false);
		}
		return wrnMessage;
	}
	
	/**
	 * Gets the back button.
	 *
	 * @return the back button
	 */
	private JButton getBackButton() {
		if (backButton == null) {
			backButton = new JButton("Back to Main");
			backButton.setBounds(250, 174, 150, 35);
		
		}
		return backButton;
	}
	
	/**
	 * ********************************* Public Methods.
	 *
	 * @param msg the msg
	 */
	
	public void displayWrnMessage(String msg){
		wrnMessage.setText(msg);
		wrnMessage.setVisible(true);
	}
	
	/**
	 * Adds the compare action listener.
	 *
	 * @param listener the listener
	 */
	public void addCompareActionListener(ActionListener listener){
		compareButton.addActionListener(listener);
	}
	
	/**
	 * Sets the comp array date.
	 *
	 * @param t the new comp array date
	 */
	public void setCompArrayDATE(ArrayList<dateModel> t)
	{
		this.compDateArray=new ArrayList<dateModel>();
		 for(int i=0;i<t.size();i++)
			 compDateArray.add( new dateModel (t.get(i).getDATE()) );
	}
	
	/**
	 * Sets the arch array date.
	 *
	 * @param t the new arch array date
	 */
	public void setArchArrayDATE(ArrayList<dateModel> t)
	{
		this.archDateArray=new ArrayList<dateModel>();
		 for(int i=0;i<t.size();i++)
			 archDateArray.add( new dateModel (t.get(i).getDATE()) );
	}
	
	/**
	 * Se pack array date.
	 *
	 * @param t the t
	 */
	public void sePackArrayDATE(ArrayList<dateModel> t)
	{
		this.packDateArray=new ArrayList<dateModel>();
		 for(int i=0;i<t.size();i++)
			 packDateArray.add( new dateModel (t.get(i).getDATE()) );
	}
	
	/**
	 * Gets the item selected index.
	 *
	 * @return the item selected index
	 */
	public int getItemSelectedIndex()
	{
		    int index;
				index=reportCombo.getSelectedIndex();				
			return	index;			
	}
		
	/**
	 * Sets the combo.
	 */
	public void setCombo()
	{	 
		reportCombo.removeAllItems();
		dateCombo.removeAllItems();		
		dateCombo2.removeAllItems();
		
		reportCombo.setVisible(true);
		dateCombo.setVisible(true);
		reportCombo.addItem("Report Type->");
		reportCombo.addItem("Complaint Report");
		reportCombo.addItem("Archive Report");
		reportCombo.addItem("Package Usage Report");
			    
		reportCombo.addItemListener(new java.awt.event.ItemListener() 
		{
			public void itemStateChanged(java.awt.event.ItemEvent e)
		    {
				String tempDate;
				int index=-1;
				System.out.println("selected "+reportCombo.getSelectedIndex()); 				
				index=reportCombo.getSelectedIndex();
				ind = index;
				if (index == 0)
				{
					dateCombo.setEnabled(false);
					dateCombo2.setEnabled(false);
				}
				
				if (index == 1)
				{
					if ( compDateArray != null)
						dateCombo.removeAllItems();
						dateCombo2.removeAllItems();
						if (DATE != null)
							DATE.clear();
						for(int i=0; i<compDateArray.size();i++)
						{
							dateCombo.setEnabled(true);
							tempDate = compDateArray.get(i).getDATE().substring(0,10);
							System.out.println ("DATE: " + tempDate);
							dateCombo.addItem (tempDate);
							dateCombo2.addItem (tempDate);
							System.out.println (tempDate);
							DATE.add (compDateArray.get(i).getDATE());
							System.out.println(compDateArray.get(i).getDATE());
						}
				}
				
				if (index == 2)
				{
					if ( archDateArray != null)
						dateCombo.removeAllItems();
						dateCombo2.removeAllItems();
						if (DATE != null)
							DATE.clear();
						for(int i=0; i<archDateArray.size();i++)
						{
							dateCombo.setEnabled(true);
							tempDate = archDateArray.get(i).getDATE().substring(0,10);
							System.out.println ("DATE: " + tempDate);
							dateCombo.addItem (tempDate);
							dateCombo2.addItem (tempDate);
							System.out.println (tempDate);
							DATE.add (archDateArray.get(i).getDATE());
							System.out.println(archDateArray.get(i).getDATE());
						}		
				}
				
				if (index == 3)
				{
					if ( packDateArray != null)
						dateCombo.removeAllItems();
						dateCombo2.removeAllItems();
						if (DATE != null)
							DATE.clear();
						for(int i=0; i<packDateArray.size();i++)
						{
							dateCombo.setEnabled(true);
							tempDate = packDateArray.get(i).getDATE().substring(0,10);
							System.out.println ("DATE: " + tempDate);
							dateCombo.addItem (tempDate);
							dateCombo2.addItem (tempDate);
							System.out.println (tempDate);
							DATE.add (packDateArray.get(i).getDATE());
							System.out.println(packDateArray.get(i).getDATE());
						}
				}
				
			}		
		});
		contentPane.repaint();
	}
	
	/**
	 * Dispose combo.
	 */
	public void disposeCombo()
	{
		if(reportCombo!=null)
		{
			reportCombo.setVisible(false);
			reportCombo.removeAllItems();
			reportCombo = null;			
		}			
	}
		
	/**
	 * Gets the index.
	 *
	 * @return the index
	 */
	public int getIndex()
	{
		return ind;
	}		
	
	/**
	 * Gets the DAT e1.
	 *
	 * @return the DAT e1
	 */
	public String getDATE1()
	{
		return DATE.get(dateCombo.getSelectedIndex());
	}
	
	/**
	 * Gets the DAT e2.
	 *
	 * @return the DAT e2
	 */
	public String getDATE2()
	{
		return DATE.get(dateCombo2.getSelectedIndex());
	}
	
	/**
	 * Gets the comp.
	 *
	 * @return the comp
	 */
	public boolean getComp()
	{
		if (rdbtnCompare.isSelected())
		   return true;
		return false;
	}
		
}